package com.atguigu.utils;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreDemo1 {
    //限流方法Semaphore
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(1);
        for (int i = 1; i < 6; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + " 号线程运行... ");
//                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }finally {
                    semaphore.release();
                }
            }, String.valueOf(i)).start();
        }
    }
}
